package com.lianniu.mgr.product.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSON;
import com.lianniu.mgr.base.BaseResult;

import com.lianniu.mgr.constants.ErrorCode;
import com.lianniu.mgr.pagination.PaginationInfo;
import com.lianniu.mgr.product.dao.IProductCateDao;
import com.lianniu.mgr.product.model.ProductCateVo;
import com.lianniu.mgr.product.service.IProductCateService;
import com.lianniu.mgr.util.StringUtil;

@Service
public class ProductCateServiceImpl implements IProductCateService {

	private final static Logger logger = Logger.getLogger(ProductCateServiceImpl.class);
	  
	@Resource
	IProductCateDao productCateDao;
	
	@Override
	public BaseResult insertProductCate(ProductCateVo curProductCateVo) {
		// TODO Auto-generated method stub
		// TODO Auto-generated method stub
	    logger.info("insertProductCate begin");
	    logger.info("insertProductCate: input data check begin");
	    logger.info("insertProductCate curProductCateVo:" +JSON.toJSONString(curProductCateVo));	   
	    
	    BaseResult baseResult;
	    
	    if (StringUtil.isEmpty(curProductCateVo.getCateName())) {
	      baseResult = BaseResult.fail(ErrorCode.ParaCheckErrorRetCode, ErrorCode.ParaCheckErrorRetInfo);
	      return baseResult;
	    }
	    if (StringUtil.isEmpty(curProductCateVo.getSortNum())) {
	      baseResult =  BaseResult.fail(ErrorCode.ParaCheckErrorRetCode, ErrorCode.ParaCheckErrorRetInfo);
	      return baseResult;
	    } 
	    
	    if (StringUtil.isEmpty(curProductCateVo.getIsBottom())) {
	    	curProductCateVo.setIsBottom("0");
		  }  
		    
	    if (StringUtil.isEmpty(curProductCateVo.getIsValidate())) {
	    	curProductCateVo.setIsValidate("0");
		  }  
	    if (StringUtil.isEmpty(curProductCateVo.getparentId())) {
	    	curProductCateVo.setparentId("0");
		  }      
	    
	    logger.info("insertProductCate: input data check end");
	    
	    return BaseResult.success(productCateDao.insertProductCate(curProductCateVo));
	}

	@Override
	public BaseResult selectByProductCateForPage(ProductCateVo curProductCateVo, PaginationInfo paginationInfo) {
		  // TODO Auto-generated method stub
	    logger.info("selectByProductCateForPage begin.");
	    if (!StringUtil.isEmpty(curProductCateVo.getCateName())) {
	    	curProductCateVo.setCateName(curProductCateVo.getCateName().replace(" ", ""));
	    }
	    logger.info("selectByProductCateForPage cateName:" +curProductCateVo.getCateName());
	    if (!StringUtil.isEmpty(curProductCateVo.getparentId())) {
	    	curProductCateVo.setparentId(curProductCateVo.getparentId().replace(" ", ""));
	    }
	    logger.info("selectByProductCateForPage parentId:" +curProductCateVo.getparentId());
	    if (!StringUtil.isEmpty(curProductCateVo.getCateId())) {
	    	curProductCateVo.setCateId(curProductCateVo.getCateId().replace(" ", ""));
	    }
	    logger.info("selectByProductCateForPage CateId:" +curProductCateVo.getCateId());
	    logger.info("selectByProductCateForPage paginationInfo:" +JSON.toJSONString(paginationInfo));
	    
	    curProductCateVo.setPaginationInfo(paginationInfo);
	    List<ProductCateVo> list=productCateDao.selectByProductCateForPage(curProductCateVo);
	    Map<String,Object> retmap=new HashMap<String,Object>();
	    retmap.put("total", paginationInfo.getTotalRecord());
	    retmap.put("curPage", paginationInfo.getCurPage());
	    retmap.put("rows", list);
	    logger.info("selectByProductCateForPage:  end");    
	    
	    return BaseResult.success(retmap); 
	}

	@Override
	public BaseResult updateById(ProductCateVo curProductCateVo) {
		// TODO Auto-generated method stub
	    logger.info("updateById begin");
	    logger.info("updateById: update data check begin");
	    logger.info("updateById curProductCate:" +JSON.toJSONString(curProductCateVo));	  
	    
	    BaseResult baseResult;
	    if (StringUtil.isEmpty(curProductCateVo.getCateName())) {
	      baseResult =BaseResult.fail(ErrorCode.ParaCheckErrorRetCode, ErrorCode.ParaCheckErrorRetInfo);
	      return baseResult;
	    }
	    if (StringUtil.isEmpty(curProductCateVo.getSortNum())) {
	      baseResult = BaseResult.fail(ErrorCode.ParaCheckErrorRetCode, ErrorCode.ParaCheckErrorRetInfo);
	      return baseResult;
	    }
	   
	    
	    logger.info("updateById: update data check end");
	    
	    return BaseResult.success(productCateDao.updateById(curProductCateVo));
	}

	@Override
	public BaseResult deleteById(String Id) {
		// TODO Auto-generated method stub
		 logger.info("deleteById begin");
		 logger.info("deleteById Id:" +Id);
		  
		 return BaseResult.success(productCateDao.deleteById(Id));
	}

	@Override
	public BaseResult selectById(ProductCateVo curProductCateVo) {
		 // TODO Auto-generated method stub
	    logger.info("selectById begin");
	    logger.info("selectById Id:" +curProductCateVo.getCateId());
	  
	    return BaseResult.success(productCateDao.selectById(curProductCateVo.getCateId()));
	}

	@Override
	public Boolean checkCateName(String cateName) {
		// TODO Auto-generated method stub
	    logger.info("checkCateName begin");
	    logger.info("checkCateName cateName:" +cateName.toString());
	    logger.info("checkCateName: input data check begin");   
	  
	    if (StringUtil.isEmpty(cateName)) {       
	      return false;
	    }
	    logger.info("checkCateName: input data check end");
	    
	    ProductCateVo productCate=new ProductCateVo();	   
	    productCate.setCateName(cateName);
	    int count=productCateDao.count(productCate);
	    if(count>0){
	      return false;      
	    }
	    else{
	      return true;
	    }   
	}

	@Override
	public BaseResult selectParentCate() {
		 // TODO Auto-generated method stub
	    logger.info("selectParentCate begin");
	    logger.info("selectParentCate parm: null");
	  
	    return BaseResult.success(productCateDao.getProductCateKeyValue());
	}

	@Override
	public BaseResult selectChildCateKV(String cateID) {
		// TODO Auto-generated method stub
	    logger.info("selectChildCateKV begin");
	    logger.info("selectChildCateKV parm:"+cateID);
	  
	    return BaseResult.success(productCateDao.getProductCateChildKv(cateID));
	}

	@Override
	public BaseResult deleteProductCateBatch(String... id) {
		 // TODO Auto-generated method stub
	    logger.info("deleteProductCateBatch begin");
	    logger.info("deleteProductCateBatch Ids:" +id.toString());
	    logger.info("deleteProductCateBatch: input data check begin");
	   
	    BaseResult baseResult;
	    if (StringUtil.isEmpty(id.toString())) {
	      baseResult =  BaseResult.fail(ErrorCode.ParaCheckErrorRetCode, ErrorCode.ParaCheckErrorRetInfo);
	      return baseResult;
	    }
	    logger.info("deleteProductCateBatch: input data check end");
	    
	    return BaseResult.success(productCateDao.deleteProductCateBatch(id));
	}

	@Override
	public BaseResult getByParentId(String parentId) {
		logger.info("getByParentId begin");
		logger.info("getByParentId parentId" +parentId);
		return BaseResult.success(productCateDao.getByParentId(parentId));
	}

    @Override
    public BaseResult selectByCateId(String cateId) {
        ProductCateVo productCateVo = productCateDao.selectByCateId(cateId);
        return BaseResult.success(productCateVo);
    }


}
